//
//  TLTabBar.m
//  Papa
//
//  Created by wf on 16/7/16.
//  Copyright © 2016年 tongli. All rights reserved.
//

#import "TLTabBar.h"

@interface TLTabBar ()

@property (nonatomic,weak) UIButton *takePhotoBtn;

@end

@implementation TLTabBar

@dynamic delegate;


- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        //初始化btn
        UIButton *btn = [[UIButton alloc] init];
        
        //设置图片
        [btn setBackgroundImage:[UIImage imageNamed:@"拍摄1"] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"拍摄1_s"] forState:UIControlStateHighlighted];
        
        //设置按钮的中间imageView图片
        // [btn setImage:[UIImage imageNamed:@"操作界面-相机按钮icon"] forState:UIControlStateNormal];
        //[btn setImage:[UIImage imageNamed:@"tabbar_compose_camera"] forState:UIControlStateHighlighted];
        //大小
        btn.size = btn.currentBackgroundImage.size;
        //btn.y = btn.currentBackgroundImage.size.height + 10;
        //添加事件
        [btn addTarget:self action:@selector(takePhotoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        self.takePhotoBtn= btn;
        
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    /// 隐藏底部 TabBar 的上横线
    for (UIView *view in self.subviews) {
        
        if ([view isKindOfClass:[UIImageView class]] && view.bounds.size.height <= 1) {
            UIImageView *line = (UIImageView *)view;
            //ima.backgroundColor = [UIColor redColor];
            line.hidden = YES;
        }
    }
    
    //调整加号按钮的位置
    self.takePhotoBtn.centerX = self.width * 0.5;
    self.takePhotoBtn.centerY = self.height * 0.5-16;
    
    //取出子控件的个数
    NSInteger count = self.subviews.count;
    
    CGFloat btnWidth = self.width / 5;
    
    NSInteger index = 0;
    
    for (int i = 0; i < count; i++) {
        //根据index调整UITabBarButton的位置
        UIView *childView = self.subviews[i];
        
        //判断是否是UITabBarButton
        if ([childView isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            //设置宽度与x值
            childView.width = btnWidth;
            childView.x = btnWidth * index;
            index++;
            
            if (index == 2) {
                index++;
            }
        }
    }
}


- (void)takePhotoBtnClick:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(tabBar:didSelectTakePhotoBtn:)]) {
        
        [self.delegate tabBar:self didSelectTakePhotoBtn:btn];
    }
    
}



@end
